home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1994 #2 / Monster Media No. 2 (Monster Media)(1994).ISO / utils1 / cmostim1.zip / CMOSTIME.CPP < prev    next >
C/C++ Source or Header  |  1994-07-30  |  3KB  |  95 lines

  1. /*    CMOSTIME Version 1.0
  2. //    Copyright (c) 1994 by David J. Arcand - All rights reserved
  3. //
  4. //    A program to read the PC's RTC and reset the BIOS clock to it.
  5. //    Typically used when running DESQview (tm) with programs that
  6. //    make intensive use of the interrupts.  When run once or twice
  7. //    a day, will make sure that you don't loose BIOS time because the
  8. //    PC was off handling other interupt and your clock stays relatively
  9. //    accurate.
  10. //
  11. //    Released as Freeware to the great unwashed masses of System Operators.
  12. //    David Arcand - July 30, 1994 on 11:22am.
  13. */
  14.  
  15. #include <dos.h>
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18.  
  19.  
  20. void main(void)
  21. {
  22.  
  23.     printf( "\nCMOSTIME Ver 1.0 by David J. Arcand. Copyright 1994 All rights reserved.\nRefreshes the DOS Time from the PC's REAL TIME CLOCK.\n");
  24.  
  25.     union    REGS regs ;
  26.     struct    dostime_t    DOSTIME;        // Destinatin of the RTC time data.
  27.     struct    dostime_t    CMOSTIME;        // Read in from the bios clock.
  28.     struct    dostime_t    DIFFTIME;        // calculating the difference.
  29.     int        DIFFSECONDS ;                // main variable for computing diff.
  30.     char    hexbuffer[8] ;                // a work area for HEX to Decimal.
  31.  
  32.     // First get the two times.
  33.  
  34.     regs.h.ah = 2 ;
  35.     int86(0x1A, ®s, ®s );                // The RTC time
  36.     _dos_gettime(&DOSTIME);                    // The BIOS time
  37.  
  38.     // Convert the HEX RTC time to decimal and a standard structure.
  39.  
  40.     sprintf( hexbuffer, "%0X\n",regs.h.ch );    // Hours
  41.     CMOSTIME.hour    = atoi( hexbuffer );
  42.  
  43.     sprintf( hexbuffer, "%0X\n",regs.h.cl );    // Minutes
  44.     CMOSTIME.minute  = atoi( hexbuffer );
  45.  
  46.     sprintf( hexbuffer, "%0X\n",regs.h.dh );    // Seconds
  47.     CMOSTIME.second  = atoi( hexbuffer );
  48.  
  49.     CMOSTIME.hsecond = 0;
  50.  
  51.  
  52.     // Make the RTC time correction.
  53.  
  54.     _dos_settime(&CMOSTIME);
  55.  
  56.  
  57.     // Print the two times.
  58.  
  59.     printf("Hardware RTC is: %2d:%02d:%02d.%02d\n",
  60.     CMOSTIME.hour, CMOSTIME.minute, CMOSTIME.second, CMOSTIME.hsecond);
  61.  
  62.     printf("  BIOS clock is: %2d:%02d:%02d.%02d\n",
  63.     DOSTIME.hour, DOSTIME.minute, DOSTIME.second, DOSTIME.hsecond);
  64.  
  65.  
  66.     // Compare the difference
  67.     if( abs(CMOSTIME.hour - DOSTIME.hour) < 5 )
  68.     {
  69.  
  70.         DIFFSECONDS      = CMOSTIME.hour   - DOSTIME.hour   ;
  71.         DIFFSECONDS     *= 60 ;
  72.  
  73.         DIFFSECONDS      = CMOSTIME.minute - DOSTIME.minute + DIFFSECONDS ;
  74.         DIFFSECONDS     *= 60 ;
  75.  
  76.         DIFFSECONDS      = CMOSTIME.second - DOSTIME.second + DIFFSECONDS ;
  77.  
  78.         DIFFTIME.hour     = DIFFSECONDS / 360 ;
  79.         DIFFSECONDS     -= DIFFTIME.hour * 360 ;
  80.  
  81.         DIFFTIME.minute  = DIFFSECONDS / 60 ;
  82.         DIFFSECONDS     -= DIFFTIME.minute * 60 ;
  83.  
  84.         DIFFTIME.second  = DIFFSECONDS ;
  85.  
  86.         printf("  Time Diff of : %2d:%02d:%02d\n",
  87.         abs(DIFFTIME.hour), abs(DIFFTIME.minute), abs(DIFFTIME.second) );
  88.     }
  89.     else printf("The times are wildly out of sync.\n" );  // Too far to even bother calculate.
  90.  
  91.     printf( "The BIOS time has been set to the RTC.\n" );    // All done.
  92.  
  93. }
  94.  
  95.